﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Pixysoft.Framework.Configurations.Core
{
    class ConfigurationNode : IConfigurationNode
    {
        string nodeName;

        string text;

        List<IConfigurationNode> nodes = new List<IConfigurationNode>();

        ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();


        public ConfigurationNode(string nodeName)
        {
            this.nodeName = nodeName;
        }


        public IConfigurationNodeCollection Node
        {
            get
            {
                return new ConfigurationNodeCollection(nodes);
            }
        }

        public IConfigurationNodesCollection Nodes
        {
            get
            {
                return new ConfigurationNodesCollection(nodes);
            }
        }

        public IConfigurationPropertyCollection Property
        {
            get
            {
                return properties;
            }
        }


        public string NodeName
        {
            get
            {
                return nodeName;
            }
        }

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        public string Base64Text
        {
            get
            {
                if (string.IsNullOrEmpty(text))
                    return null;

                return Pixysoft.Security.Base64.Decript(text.Trim());
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    this.text = null;

                    return;
                }

                this.text = Pixysoft.Security.Base64.Encript(value.Trim());
            }
        }

        public string GetDesText(string key)
        {
            if (string.IsNullOrEmpty(key))
                throw new Exception("missing des key.");

            if (string.IsNullOrEmpty(text))
                return null;

            return Pixysoft.Security.DES.Decrypt(
                Pixysoft.Security.Base64.Decript(text), key);
        }

        public void SetDesText(string key, string value)
        {
            if (string.IsNullOrEmpty(key))
                return;

            if (string.IsNullOrEmpty(value))
            {
                this.text = null;

                return;
            }

            value = value.Trim();

            this.text = Pixysoft.Security.Base64.Encript(
                Pixysoft.Security.DES.Encrypt(value, key));
        }

        public T Create<T>()
        {
            string filename = this.Property[StringHelper.default_attribute_filename];

            string classname = this.Property[StringHelper.default_attribute_classname];

            Assembly assembly = null;

            try
            {
                assembly = Assembly.LoadFrom(filename);
            }
            catch
            {
                throw new Exception("missing file, fail load assembly. filename - " + filename);
            }

            if (assembly == null)
            {
                throw new Exception("fail load assembly. filename - " + filename);
            }

            try
            {
                T instance = (T)assembly.CreateInstance(classname);

                return instance;
            }
            catch
            {
                throw new Exception(
                    string.Format("fail to create instance, filename = {0}, classname = {1}",
                    filename, classname));
            }
        }


        public override string ToString()
        {
            return nodeName;
        }
    }
}
